package com.supermap.dataservice.cloud.productC;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.productC.ProductCloudService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年12月04 下午 2:39
 * @email 1301584033@qq.com
 */
@Service
public class ProductCloudServiceImpl implements ProductCloudService {

    @Resource
    private ProductCloudMapper productCloudMapper;

    @Override
    public JSONUtil getGroupProduct(Integer pageNum,Integer pageSize) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        pageNum=(pageNum-1)*pageSize;
        return new JSONUtil().setData(productCloudMapper.getGroupProduct(pageNum,pageSize)).setCountSum(productCloudMapper.getGroupProductCount());
    }

    @Override
    public Map<String,Object> getProductCountSumBycropId(String cropId) {

        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String  cropName=  productCloudMapper. getCropNameBuCropId(cropId);
        Integer  counSum1=   productCloudMapper.getExpertSuggestBycrop(cropId);
        String cropstr=productCloudMapper.getCropGroupConcat(cropId);
        cropstr=cropstr.replace(",","\",\"");
        cropstr="\""+cropstr+"\"";
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        Integer counSum=counSum1+productCloudMapper.getExpertProductCounSum(cropstr);
        Integer countSumProduct= productCloudMapper.getProductByCropName(cropName);
        Map<String,Object> map = new HashMap<>();
        map.put("公共服务产品",countSumProduct);
        map.put("专家产品",counSum);
        return map;
    }


    @Override
    public JSONUtil getCqGroupAreaFarmCorpCountSum(String crop) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(productCloudMapper.getCqGroupAreaFarmCorpCountSum(crop));
    }

    @Override
    public JSONUtil getExpertSuggestBycropCount(String areaCode,String crop, Integer pageSize) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(productCloudMapper.getExpertSuggestBycropCount(areaCode,crop,pageSize));
    }
}
